﻿Imports CodeMatrix.Context
Imports System.Linq

Partial Class Apps_Shares_Uc_master_department_fv
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        'Change mode by querystring
        If Not String.IsNullOrEmpty(Request.QueryString("ReadOnly")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 1)
            fv.ChangeMode(FormViewMode.ReadOnly)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Insert")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 2)
            fv.ChangeMode(FormViewMode.Insert)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Edit")) Or String.IsNullOrEmpty(Request.QueryString("Update")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 3)
            fv.ChangeMode(FormViewMode.Edit)
        End If
    End Sub

    Protected Sub fv_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles fv.ItemInserted
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log insert
                GenericHelper.GenerateScript(GenerateScriptLogType.Insert, LogMenuType.master_department, e.Values)
                GenericHelper.ShowAlertSuccessful(Me)
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInInsertMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fv.ItemInserting
        Try
            ' Server validation ---------------------------
            If e.Values("master_department_name").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชือระดับหน่วยงาน")
            End If

            If e.Values("master_department_level") = 0 Then
                Throw New Exception("กรุณาเลือกระดับหน่วยงาน")
            End If
            ' Server validation ---------------------------

            Dim _lastLevel As Int32 = 0
            Dim ddlLevel As DropDownList = fv.FindControl("ddlLevel")
            _lastLevel = ddlLevel.SelectedValue

            Dim ctx = New DataClassesDataContext
            If _lastLevel = 10 Then
                Dim count = ctx.master_departments.Count(Function(f) f.master_department_level = 10 And f.master_department_status = 1)
                If count > 0 Then
                    e.Cancel = True
                    GenericHelper.ShowAlert(Me, "มีระดับต่ำสุดที่มีอยู่แล้ว")
                End If
            Else
                Dim cLvl = ctx.master_departments.Where(Function(f) f.master_department_level >= _lastLevel)
                For Each i In cLvl
                    i.master_department_level += 1
                Next
                ctx.SubmitChanges()
            End If
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fv.ItemUpdated
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log update
                GenericHelper.GenerateScript(GenerateScriptLogType.Update, LogMenuType.master_department, e.NewValues, e.OldValues)
                'Redirect to list page
                Response.Redirect("master_department_list.aspx?Edit=true")
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInEditMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fv.ItemUpdating
        Try
            ' Server validation ---------------------------
            If e.NewValues("master_department_name").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชือระดับหน่วยงาน")
            End If
            ' Server validation ---------------------------
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub
End Class
